from fastapi import UploadFile
from sqlalchemy.orm import Session
from app.db.db_models import FileModel, 
from typing import Optional
from pathlib import Path

class DBFile:
    def __init__(self, session: Session):
        self.session = session

    def upload_file(self, file: UploadFile):
        file_model = FileModel(filename=file.filename, data=file.file.read())
        self.session.add(file_model)
        self.session.commit()
        self.session.refresh(file_model)
        return file_model

    def download_file(self, file_name: str):
        file_model = self.session.query(FileModel).filter(FileModel.filename == file_name).first()
        if file_model:
            return file_model.data
        return None

def get_video_path(video_id: str) -> Path:
    """
    获取视频文件路径
    """
    videos_dir = Path(__file__).resolve().parent.parent.parent / "videos"
    video_file = videos_dir / f"{video_id}.mp4"
    return video_file if video_file.exists() else None
